﻿using JianZhangTool.log;
using Newtonsoft.Json.Linq;
using System;

namespace JianZhangTool.net
{
    public class BiliDanmuMessage
    {

        /// <summary>
        /// 获取航海字串
        /// </summary>
        /// <param name="guard_lv"></param>
        /// <returns></returns>
        private static string get_guard_str(int guard_lv)
        {
            if (guard_lv == 1)
            {
                return "总督";
            }
            if (guard_lv == 2)
            {
                return "提督";
            }
            if (guard_lv == 3)
            {
                return "舰长";
            }
            return "未知(" + guard_lv + ")";
        }

        /// <summary>
        /// 消息解析
        /// </summary>
        /// <param name="bili_msg_json"></param>
        public static void OnMsg(JObject bili_msg_json)
        {
            if (Main.Instance.GetRoomGuardInfos() == null)
            {
                return;
            }
            try
            {
                string cmd = bili_msg_json["cmd"].ToString();
                /*if (!cmd.Equals("INTERACT_WORD") && !cmd.Equals("DANMU_MSG"))
                {
                    Main.Instance.add_log("[debug] cmd = " + cmd);
                }*/
                switch (cmd)
                {
                    // 航海
                    case "GUARD_BUY":
                        {
                            int uid = bili_msg_json["data"]["uid"].Value<int>();
                            string uname = bili_msg_json["data"]["username"].ToString();
                            int guard_lv = bili_msg_json["data"]["guard_level"].Value<int>();
                            int num = bili_msg_json["data"]["num"].Value<int>();
                            // 记录上舰
                            Main.Instance.GetRoomGuardInfos().OnGuardBuy(uid, uname, guard_lv, num);

                            string log_str = "[log][" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]" + uname + "(" + uid + ")" + " 上船! " + get_guard_str(guard_lv) + " x " + num;
                            // 主面板日志
                            Main.Instance.add_log(log_str);
                            // 日志文件
                            Logger.log("guard", log_str);
                        }
                        break;
                    // 礼物
                    case "SEND_GIFT":
                        {
                            int uid = bili_msg_json["data"]["uid"].ToObject<int>();
                            string uname = bili_msg_json["data"]["uname"].ToString();
                            string gift = bili_msg_json["data"]["giftName"].ToString();
                            int count = bili_msg_json["data"]["num"].ToObject<int>();

                            string log_str = "[log][" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]" + uname + "(" + uid + ")" + " 赠送了 " + count + " 个 " + gift;
                            // 主面板日志
                            Main.Instance.add_log(log_str);
                            // 日志文件
                            Logger.log("gift", log_str);
                        }
                        break;
                    /*case "COMBO_SEND":
                        {
                            int uid = bili_msg_json["data"]["uid"].ToObject<int>();
                            string uname = bili_msg_json["data"]["uname"].ToString();
                            string gift = bili_msg_json["data"]["gift_name"].ToString();
                            int count = bili_msg_json["data"]["combo_num"].ToObject<int>();

                            string log_str = "[log][" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]" + uname + "(" + uid + ")" + gift + " " + count + " 连击";
                            // 主面板日志
                            Main.Instance.add_log(log_str);
                            // 日志文件
                            Logger.log("gift", log_str);
                        }
                        break*/;
                    // SC
                    case "SUPER_CHAT_MESSAGE":
                    case "SUPER_CHAT_MESSAGE_JP":
                        {
                            int uid = bili_msg_json["data"]["uid"].ToObject<int>();
                            string uname = bili_msg_json["data"]["user_info"]["uname"].ToString();
                            string msg = bili_msg_json["data"]["message"]?.ToString();
                            decimal price = bili_msg_json["data"]["price"].ToObject<decimal>();

                            string log_str = "[log][" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]" + uname + "(" + uid + ")" + " 发送了SC(¥" + price + ") : " + msg;
                            // 主面板日志
                            Main.Instance.add_log(log_str);
                            // 日志文件
                            Logger.log("sc", log_str);
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Logger.log("error", e.StackTrace);
            }
        }
    }
}
